<?php
if(!defined('DCS_ROOT')) {exit('Access Denied');} 

class profile {

	function __construct() {
		$this->url = getUrl();
		$action = $this->url['params']['1'];
		require_once(APP_PATH."model/admin/init.php");
		if($action == 'add'){
			exit($this->addAction());
		}elseif($action == 'del'){
			exit($this->del());
		}elseif($action == 'list'){
			exit($this->listAction());
		}elseif($action == 'edit'){
			exit($this->editAction());
		}else{
			$this->defaultAction();
			exit();
		}
	}

	function defaultAction(){
		if(!isPost()){
			$tpl = new Template("admin/profile/main.htm" );
			require_once(APP_PATH."model/admin/profile.php" );
			$DB  = getDB();
			$username = getAdmin();
			
			$data = new Profile_Model ($DB);
			$adminUser = $data->getAdminUser($username);
			
			$adminUser['username'] = $username;

			$sql = $DB->query("SELECT * FROM admin WHERE adminuser = '$username' ");		
			$username = $DB->fetchRow($sql);
			
			
			$adminUser['lasttime'] = date("Y-m-d H:i:s",$username['logintime']);
			$adminUser['lastip'] = $username['loginip'];
			$adminUser['loginnum'] = $username['loginnum'];	
	
			$tpl->assign($adminUser);
			$tpl->output();
		}else{		
			$id =  $_POST['admin_user_id'];
			$adminuser =  $_POST['adminuser'];	
			$new_pwd = $_POST['newpasswd'];
			$confirm_passwd = $_POST['confirm_passwd'];

			if(empty($id) && empty($confirm_passwd) && empty($new_pwd)){
				message('请在输入框中输入信息');
			}
			
			if($new_pwd <> $confirm_passwd){
				message('两次密码不相同。');
			}
			
			require_once(APP_PATH."model/admin/profile.php" );
			$DB  = getDB();
			$data = new Profile_Model($DB);			
			$edituser = $data->edit($id,$new_pwd);
			if(empty($edituser)){
				message('修改密码失败！');
			}else{
				session_start();
				unset($_SESSION['adminuser']);
				message('修改密码成功！');
			}
		}
	}

	function addAction(){
		require_once(APP_PATH."Model/Profile.php" );
		$DB  = getDB();
		$data = new Profile_Model ($DB);

		if(!isPost()){
			$tpl = new Template( "admin/profile/add.htm" );
			$tpl->output();
		}else{
			$username = trim($_POST["username"]);	
			$password = trim($_POST["password"]);
			$is_superadmin = trim($_POST['is_superadmin']);
			
			if(empty($username) AND empty($password)) {
				message('请填写必填项目。',ADMIN.'profile/add'.$id);
				exit();
			}			
			if (!preg_match ("/^[a-zA_Z0-9_\-]+$/i", $username)) {
				message('对不起，您输的用户名不合法，请返回修改。',ADMIN.'profile/add'.$id);
			}

			$is_have = $data->getAdminUser($username);
			if($is_have){
				message('已存在此管理员',ADMIN.'profile/add');
			}
			$add = $data->addUser($username,$password);
			if ($add){
				$message = '成功增加一个用户。';
			}else{
				$message = '增加一个用户失败！';
			}
			message($message,ADMIN.'profile/add',$message,'2');			
		}
	}

	function editAction(){
		require_once(APP_PATH."Model/Profile.php" );
		$DB  = getDB();
		$data = new Profile_Model ($DB);
		$userid = $this->url['params']['2'];
		$adminUser = $data->getId($userid);
		if(!isPost()){
			$tpl = new Template("admin/profile/edit.htm");			
			$tpl->assign($adminUser);
			$tpl->output();
		}else{
			$username = trim($_POST["username"]);	
			$password = trim($_POST["password"]);
			$is_superadmin = trim($_POST['is_superadmin']);
			
			if(empty($username) AND empty($password)) {
				message('请填写必填项目。',ADMIN.'profile/list'.$id);
				exit();
			}			
			if (!preg_match ("/^[a-zA_Z0-9_\-]+$/i", $username)) {
				message('对不起，您输的用户名不合法，请返回修改。',ADMIN.'profile/list'.$id);
			}
			
			if(md5($password) != $adminUser['password']){
				$password = md5($password);
			}

			$add = $data->editUser($userid,$username,$password);
			if ($add){
				$message = '修改用户成功。';
			}else{
				$message = '修改用户失败！';
			}
			message($message,ADMIN.'profile/list',$message,'2');			
		}
	}

	function listAction () {
		require_once(APP_PATH."Model/Profile.php" );
		$DB  = getDB();
		$data = new Profile_Model ($DB);		
		$tpl = new Template( "admin/profile/list.htm" );

		if($this->url['params']['1'] == 'page'){
				$pagetemp = $this->url['params']['2'];		
		}		
		$condition['page']= is_numeric($pagetemp) ? $pagetemp :'1';

		$condition['rows'] = '20';
		$condition['order'] = 'id';
		if(isPost()){
			$keyword = htmlClear(addslashes(trim($_POST['keyword'])));
			if(!empty($keyword)){
				$condition['condition'] = " adminuser LIKE '%$keyword%' ";
			}else{
				message('请输入要搜索的关键字！');
			}
			if (strlen($keyword) < 2) {
				message('关键字不能少于2个字符！');
			}
		}
		$adminList = $data->getList($condition);
		foreach($adminList as $k => $v ){
			$adminList[$k]['logintime'] = date("Y-n-j  G:i",$v['logintime']);
			$adminList[$k]['lasttime'] = date("Y-n-j G:i:s",$v['lasttime']);
		}
		$tpl->assign('adminList', $adminList);

		$pageParam['pageUrl'] = ADMIN.'profile/page/';
		$pageParam['recordCount']	= $data->total($condition);
		$pageParam['pageCount']		= ceil( $pageParam['recordCount'] / $condition['rows'] );
		$pageParam['pageSize']		= $condition['rows'];
		$pageParam['currentPage']	= $condition['page'];
		$tpl->assign( "pageParam", makePage( $pageParam ) );
		$tpl->output();	
		
	}

	function del(){
		require_once(APP_PATH."Model/Profile.php" );
		$DB  = getDB();
		$data = new Profile_Model ($DB);
		$userid = $this->url['params']['2'];
		if(!is_numeric($userid)){
			exit('找不到此用户。');
		}
		$adminUser = $data->getId($userid);
		if(!isPost()){
			$tpl = new Template( "admin/profile/del.htm" );
			$tpl->assign($adminUser);
			$tpl->output();
		}else{			
			$del = $data->delUser($userid);
			if ($del){
				$message = '删除管理员成功。';
			}else{
				$message = '删除管理员失败！';
			}
			exit($message);
		}
	}




}
?>
